package com.ruoyi.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.DeletedConstants;

import com.ruoyi.common.core.constant.MqConstants;
import com.ruoyi.edu.api.domain.EduTeacher;
import com.ruoyi.edu.mapper.EduTeacherMapper;
import com.ruoyi.edu.service.IEduTeacherService;
import com.ruoyi.order.api.domain.dto.TeacherNameDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 讲师Service业务层处理
 *
 * @author isahino
 * @date 2023-09-14
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements IEduTeacherService {
    @Resource
    private EduTeacherMapper eduTeacherMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询讲师
     *
     * @param id 讲师主键
     * @return 讲师
     */
    @Override
    public EduTeacher selectEduTeacherById(String id) {
        return eduTeacherMapper.selectEduTeacherById(id);
    }

    /**
     * 查询讲师列表
     *
     * @param eduTeacher 讲师
     * @return 讲师
     */
    @Override
    public List<EduTeacher> selectEduTeacherList(EduTeacher eduTeacher) {
        return eduTeacherMapper.selectEduTeacherList(eduTeacher);
    }

    /**
     * 新增讲师
     *
     * @param eduTeacher 讲师
     * @return 结果
     */
    @Override
    @CacheEvict(value = "teacher", allEntries = true)
    public int insertEduTeacher(EduTeacher eduTeacher) {
        eduTeacher.setIsDeleted(DeletedConstants.UN_DELETED);
        eduTeacher.setGmtCreate(new Date());
        eduTeacher.setGmtModified(new Date());
        return eduTeacherMapper.insert(eduTeacher);
    }

    /**
     * 修改讲师
     *
     * @param eduTeacher 讲师
     * @return 结果
     */
    @Override
    @CacheEvict(value = "teacher", allEntries = true)
    public int updateEduTeacher(EduTeacher eduTeacher) {
        // 若老师名称有变化，则修改订单对应消息
        EduTeacher byId = getById(eduTeacher);
        if (!byId.getName().equals(eduTeacher.getName())) {
            //  发送异步信息给订单微服务，修改对应老师姓名
            TeacherNameDto teacherNameDto = new TeacherNameDto();
            teacherNameDto.setOldName(byId.getName());
            teacherNameDto.setNewName(eduTeacher.getName());
            rabbitTemplate.convertAndSend(MqConstants.EDU_EXCHANGE_DIRECT, MqConstants.TEACHER_QUEUE, teacherNameDto);
        }
        return eduTeacherMapper.updateEduTeacher(eduTeacher);
    }

    /**
     * 批量删除讲师
     *
     * @param ids 需要删除的讲师主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "teacher", allEntries = true)
    public int deleteEduTeacherByIds(String[] ids) {
        return eduTeacherMapper.deleteEduTeacherByIds(ids);
    }

    /**
     * 删除讲师信息
     *
     * @param id 讲师主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "teacher", allEntries = true)
    public int deleteEduTeacherById(String id) {
        return eduTeacherMapper.deleteEduTeacherById(id);
    }

    @Override
    @Cacheable(value = "teacher", key = CacheConstants.HOT_TEACHER_LIST)
    public List<EduTeacher> findHotTeacher() {
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(EduTeacher::getSort);
        queryWrapper.eq(EduTeacher::getIsDeleted, DeletedConstants.UN_DELETED);
        queryWrapper.last("limit 4");
        return eduTeacherMapper.selectList(queryWrapper);
    }
}
